package ua.crimea.bat.ss.utils;

import ua.crimea.bat.ss.exceptions.SSException;

import java.util.*;

/**
 * <p/>
 * <p/>
 * Original Author:   <a href="mailto:Alexey Batyuta <alexey@pilgrimcs.com.ua>">Alexey Batyuta</a><br/>
 * Created Date:     Oct 12, 2010 <br/>
 * Created Time:     9:55:04 AM <br/>
 * <p/>
 * $Id: CollectionUtil.java 59 2010-10-12 08:29:50Z alexey.batyuta@gmail.com $
 * <p/>
 * $Author: alexey.batyuta@gmail.com $
 * $Date: 2010-10-12 08:29:50 +0000 (Tue, 12 Oct 2010) $
 * $Rev: 59 $
 * $URL: http://bat-test-docs.googlecode.com/svn/trunk/core/src/main/java/ua/crimea/bat/ss/utils/CollectionUtil.java $
 */

public class CollectionUtil {
    @SuppressWarnings("unchecked")
    public static <T> List<T> cast(List list) {
        return list;
    }

    @SuppressWarnings("unchecked")
    public static <T> Collection<T> cast(Collection list) {
        return list;
    }

    @SuppressWarnings("unchecked")
    public static <K, V> Map<K, V> cast(Map map) {
        return map;
    }

    @SuppressWarnings("unchecked")
    public static <K, V> HashMap<K, V> cast(HashMap map) {
        return map;
    }

    public static <T extends Enum<T>> EnumSet<T> convert(Collection<T> collection, Class<T> elementType) {
        EnumSet<T> result = EnumSet.noneOf(elementType);
        if (collection != null) {
            for (Object t : collection.toArray()) {
                if (t != null) {
                    result.add(Enum.valueOf(elementType, t.toString()));
                }
            }
        }
        return result;
    }

    public static <T extends Enum<T>> EnumSet<T> convert(Set<String> collection, Class<T> elementType) {
        EnumSet<T> result = EnumSet.noneOf(elementType);
        if (collection != null) {
            for (String t : collection) {
                if (t != null) {
                    result.add(Enum.valueOf(elementType, t));
                }
            }
        }
        return result;
    }

    public static boolean isNullOrEmpty(Collection list) {
        return list == null || list.size() == 0;
    }

    public static <T> List<T> filter(Collection<T> source, IPredicate<T> predicate) throws SSException {
        List<T> result = new ArrayList<T>(source.size());
        if (!isNullOrEmpty(source)) {
            for (final T item : source) {
                if (predicate.ok(item)) {
                    result.add(item);
                }
            }
        }
        return result;
    }

    public static <T> void remove(Collection<T> source, IPredicate<T> predicate) throws SSException {
        if (!isNullOrEmpty(source)) {
            Iterator<T> iterator = source.iterator();
            while (iterator.hasNext()) {
                T item = iterator.next();
                if (predicate.ok(item)) {
                    iterator.remove();
                }
            }
        }
    }

    public static <T> boolean contains(Collection<T> items, IPredicate<T> predicate) throws SSException {
        if (!isNullOrEmpty(items)) {
            for (final T item : items) {
                if (predicate.ok(item)) {
                    return true;
                }
            }
        }
        return false;
    }
}